package graphicUserInterface.testAndCalibrationInterfaces.screens;

import graphicUserInterface.elements.WebCamDisplay;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CalibrationToolScreen extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1497045403403127643L;
	
	/* *********************************************************
	 * Attributes
	 * ********************************************************* */
	private JPanel _leftPart;
	private JPanel _rightPart;
	private WebCamDisplay _webCamContainer;
	private int _margin = 5;
	private int _low_margin = 500;
	private int _high_margin = 307000;
	private Color _average;
	private boolean  _isOK = false;
	
	
	/* *********************************************************
	 * Constructor(s)
	 * ********************************************************* */
	/**
	 * 
	 */
	public CalibrationToolScreen(){
		super();
		System.out.println("calibrationToolScreen creation");
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		_webCamContainer = new WebCamDisplay();
		_leftPart = new JPanel();
		this.drawCam();
		_rightPart = new JPanel();
		this.drawTrackingInfo();
		
		this.add(_leftPart);
		
		this.add(_rightPart);
		
		this.validate();
		System.out.println("calibrationToolScreen creation over");
	}
	
	/* *********************************************************
	 * Instance Methods
	 * ********************************************************* */

	

	private void drawTrackingInfo() {
		_rightPart.add(new JLabel("to calibrate put the back of your hand behind the yellow square"));
		JButton _calibrate = new JButton("calibrate");
		


		_calibrate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					System.out.println("Game on!!\n");

					BufferedImage image = engine.videoCapture.GrabFrame
						.doGrabFrame(_webCamContainer.getFrameGrabbingControl());
					
				    _average = engine.motionDetection.Calibration.calibrate(image);
					_isOK = engine.motionDetection.Calibration.isCalibrationOK(image, _margin, _average, _low_margin, _high_margin);
					if(_isOK){
						System.out.println("You won!!\n");
					}
					else{
						System.out.println("Game over!!\n");
					}
			}
		});

		_rightPart.add(_calibrate);
		
		//something to modify the margin for the color
		
		//something to modify the margins for the number of pixels
		
		//a button to launch the calibration
		
		//the 3 colors
		
		//if the calibration was ok
		
		
		
	}

	/**
	 * 
	 */
	private void drawCam() {
		_leftPart.add(_webCamContainer);
		_webCamContainer.setCalibrationZone(true);
	}
}
